package com.wgz.ai.langchain4jai.memory;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;

/**
 * @Title: WGZ
 * @Package com.wgz.ai.langchain4jai
 * @Date 2025/3/4 22:35
 * @description: 使用AiServices时的聊天记忆
 */
public class ServiceWithMemoryExample {
    private final static String apiKey = "sk-svcacct-J-zoI616zoIN8ghM6amdG_SbFNc3vADd5yRa-53EQkr-lPN0tjD8QgWJfAmSuT3BlbkFJxBhbhopzngM8ZUB8KwByRFPnJ5bDUT1eq2Kop0OlggeqfzdsNSF45DQFZ94mgA";

    interface  Assistant{
        String chat(String message);
    }

    public static void main(String[] args) {
        // 创建一个 ChatMemory（聊天记忆），用于存储最近 10 条对话信息
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);

        // 创建 OpenAI 聊天模型
        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("gpt-3.5-turbo")
                .build();

        // 创建 AI 助手，并绑定聊天模型和聊天记忆
        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
                .chatMemory(chatMemory)
                .build();

        String answer = assistant.chat("hello! my name is klaus.");
        System.out.println(answer);

        // 发送第二条消息 "What is my name?" 让 AI 记住并回答用户的名字
        String answerWithName = assistant.chat("What is my name?");
        System.out.println(answerWithName); // 预期输出：Your name is Klaus.

    }
}
